<?php
    include_once ("MySql.php");
    include_once ("../entity/ProductoAlmacen.php");

    class ProductoAlmacenDA extends dal{
        public function ProductoAlmacenDA(){
            parent::dal();
        }
        public function insertar($entity){
            $sql = "CALL SGLSI_PRODUCTOALMACEN('$entity->cod_producto',"
                    ."'$entity->cod_almacen',"
                    ."'$entity->cod_proveedor',"
                    ."'$entity->stock_max',"
                    ."'$entity->stock_min',"
                    ."'$entity->stock_actual',"
                    ."'$entity->usuario_crea',"
                    ."'$entity->plazo_abastecimiento',"
                    ."'$entity->estado_max')";
            
            $result = parent::ejecutar($sql);
            $dato = 0;
            if($result){
                while($fila = mysqli_fetch_object($result)){
                    $dato = $fila->resultado;
                }
            }
            return $dato;
        }
        public function validarGrabar($codigo){
            $sql = "CALL SGLSS_VALIDAR_ELIMINAR_PRODUCTO('$codigo')";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $num_row = $fila->num_row;
            }
            return $num_row;
        }
        public function validarEliminar($codigo){
            $sql = "CALL SGLSS_VALIDAR_ELIMINAR_PRODALMACEN('$codigo')";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $num_row = $fila->num_row;
            }
            return $num_row;
        }
        public function eliminar($entity){
            $sql = "CALL SGLSD_PRODUCTOALMACEN('$entity->codigo')";
            $result = parent::ejecutar($sql);
            if($result){
                return true;
            }else{
                return false;
            }
        }

        public function numeroRegistro($codigo,$producto,$cod_almacen){
            $sql = "CALL SGLSS_CUENTA_PRODUCTOALMACEN('$codigo','$producto','$cod_almacen')";
            $result = parent::ejecutar($sql);
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        public function BuscarSearch($entity, $start , $limit,$sidx,$sord){
            $sql = "CALL SGLSS_PRODUCTOALMACEN_SEARCH('$entity->codigo',"
                    . "'$entity->producto',"
                    . "'$entity->cod_almacen',"
                    . "'$start' ,"
                    . "'$limit',"
                    . "'$sidx',"
                    . "'$sord')";
            $result = parent::ejecutar($sql);
            while($fila = mysqli_fetch_object($result)){
                $obj = new ProductoAlmacen();
                $obj->codigo = $fila->codigo;
                $obj->cod_almacen = $fila->cod_almacen;
                $obj->almacen = $fila->almacen;
                $obj->cod_producto = $fila->cod_producto;
                $obj->producto = $fila->producto;
                $obj->stock_min = $fila->stock_min;
                $obj->stock_max = $fila->stock_max;
                $obj->stock_actual = $fila->stock_actual;
                $obj->cod_proveedor = $fila->cod_proveedor;
                $obj->proveedor = $fila->proveedor;
                $obj->fecha_crea = $fila->fecha_crea;
                $productoAlmacen[] = $obj;
            }
            return $productoAlmacen;
        }
        public function contarStockMinimo(){
            $sql = "CALL SGLSS_PRODUCTOALMACEN_COUNT";
            $result = parent::ejecutar($sql);
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        public function numeroRegistro1(){
            $sql = "CALL SGLSS_PRODUCTOALMACEN_COUNT";
            $result = parent::ejecutar($sql);
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        public function BuscarSearch1($start , $limit,$sidx,$sord){
            $sql = "CALL SGLSS_PRODUCTOALMACEN_STOCKMIN_SEARCH('$start',"
                    . "'$limit',"
                    . "'$sidx',"
                    . "'$sord')";
            $result = parent::ejecutar($sql);
            while($fila = mysqli_fetch_object($result)){
                $obj = new ProductoAlmacen();
                $obj->codigo = $fila->codigo;
                $obj->cod_almacen = $fila->cod_almacen;
                $obj->almacen = $fila->almacen;
                $obj->cod_producto = $fila->cod_producto;
                $obj->producto = $fila->producto;
                $obj->stock_min = $fila->stock_min;
                $obj->stock_max = $fila->stock_max;
                $obj->stock_actual = $fila->stock_actual;
                $obj->cod_proveedor = $fila->cod_proveedor;
                $obj->proveedor = $fila->proveedor;
                $obj->fecha_crea = $fila->fecha_crea;
                $productoAlmacen[] = $obj;
            }
            return $productoAlmacen;
        }
    }
?>